package com.mls.fivedistrict.controller.portal;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.Advertising;
import com.mls.fivedistrict.server.AdvertisingService;

import com.mls.fivedistrict.server.NewsFlashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chenxiuxiang on 2018/4/14.
 */
@RestController
@RequestMapping("/api/app")
public class AdvertisingController extends BaseController {

    @Autowired
    AdvertisingService advertisingService;

    @Autowired
    NewsFlashService newsFlashService;

    @GetMapping("/v1/adversings")
    public ServiceResponse getAdversingList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        Wrapper wrapper = pageFilter.getWrapper();
        if (wrapper == null) {
            wrapper = Condition.create();
        }
        wrapper.orderBy("orders", false);
        wrapper.eq("type", "home");
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Advertising> adversionList = advertisingService.selectList(wrapper);
        PageInfo pageInfo = new PageInfo(adversionList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v2/adversings")
    public ServiceResponse getAdversingListV2() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Advertising> adversionList = advertisingService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(adversionList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
}
